/*
 * Copyright (c) 2010, 2012 Luan Nguyen Thanh. All rights reserved.
 * 
 * This code is written by Luan Nguyen Thanh, with any partners involved.
 * The use of this code should not be permitted!
 * If you want to use, please do contact me!
 */
package MyIM.client.view.component;

import MyIM.client.engine.Engine;
import MyIM.client.view.ContactsView;
import MyIM.client.view.form.Dialog_ChangeGroup;
import MyIM.client.view.form.Dialog_EditGroup;
import MyIM.component.Friend;
import MyIM.component.Group;
import MyIM.utilities.Status;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;

/**
 *
 * @author Luan Nguyen Thanh <tklarryonline@gmail.com>
 */
public class FriendsPopupMenu extends JPopupMenu implements ActionListener {

    private Engine engine;
    private Friend friend;
    private Group group;
    private ContactsView contactsView;

    public FriendsPopupMenu(ContactsView contactsView, Engine engine, Friend friend) {
        this.contactsView = contactsView;
        this.engine = engine;
        this.friend = friend;
        this.group = null;
        initFriend();
    }

    public FriendsPopupMenu(ContactsView contactsView, Engine engine, Group group) {
        this.contactsView = contactsView;
        this.engine = engine;
        this.friend = null;
        this.group = group;
        initGroup();
    }

    private void initFriend() {
        JMenuItem changeGroup = makeMenuItem("Change group", KeyEvent.VK_C, 0,
                0, "change_group");
//        changeGroup.addActionListener(null);
        add(changeGroup);

        JMenuItem deleteFriend = makeMenuItem("Delete friend", KeyEvent.VK_D, 0,
                0, "delete_friend");
//        deleteFriend.addActionListener(null);
        add(deleteFriend);
    }

    private void initGroup() {
        JMenuItem renameGroup = makeMenuItem("Rename group", KeyEvent.VK_C, 0,
                0, "rename_group");
//        changeGroup.addActionListener(null);
        add(renameGroup);

        JMenuItem deleteGroup = makeMenuItem("Delete group", KeyEvent.VK_D, 0,
                0, "delete_group");
//        deleteFriend.addActionListener(null);
        add(deleteGroup);
    }

    /**
     * Creates a non Iconized MenuItem based on the input info.
     *
     * @param name The name of the menu item.
     * @param key The Mnemonic KeyEvent for this menu item.
     * @param ac The Accelerator KeyEvent for this menu item.
     * @param mask The Mask (Ctrl, Shift or Alt) for this menu item.
     * @param cmd The ActionCommand for this menu item. Cannot be null.
     * @return The created MenuItem
     */
    private JMenuItem makeMenuItem(String name, int key, int ac,
            int mask, String cmd) {
        JMenuItem m = new JMenuItem(name, key);

        // Specifies a standard menu item.
        if (ac != 0) {
            m.setAccelerator(KeyStroke.getKeyStroke(ac, mask));
        }
        m.setActionCommand(cmd);
        m.addActionListener(this);

        return m;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        if (cmd.equalsIgnoreCase("change_group")) {
            System.out.println("change group name");
            changeFriendGroup();
        } else if (cmd.equalsIgnoreCase("delete_friend")) {
            deleteFriend();

        } else if (cmd.equalsIgnoreCase("rename_group")) {
            renameGroup();
        } else if (cmd.equalsIgnoreCase("delete_group")) {
            System.out.println("Delete Group");
            deleteGroup();
        }
    }

    private void changeFriendGroup() {
        new Dialog_ChangeGroup(friend, contactsView, engine);
    }

    private void deleteFriend() {
        try {
            Status s = engine.getServer().removeFriend(engine.getClient().getUID(), friend.getUid());
            if (s == Status.SUCCESS) {
                contactsView.resetFriendsList();
            } else {
                JOptionPane.showMessageDialog(this, "Cannot Delete The Friend");
            }
        } catch (RemoteException ex) {
            Logger.getLogger(FriendsPopupMenu.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void renameGroup() {
        new Dialog_EditGroup(group, contactsView, engine);
    }

    private void deleteGroup() {
        try {
            Status s = engine.getServer().removeGroup(engine.getClient().getUID(), group.getGroupId());
            if (s == Status.SUCCESS) {
                contactsView.resetFriendsList();
            } else {
                JOptionPane.showMessageDialog(this, "Cannot Delete The Group");
            }
        } catch (RemoteException ex) {
            Logger.getLogger(FriendsPopupMenu.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
